﻿using AutoWelding.BLL;
using AutoWelding.Models;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AutoWelding.Views.Alarm
{
    public partial class AlarmForm : Form
    {
        public AlarmForm()
        {
            InitializeComponent();
        }

        private AlarmBLL alarmBLL = new AlarmBLL();

        private void AlarmForm_Load(object sender, EventArgs e)
        {
            //显示指定列  不指定的不显示
            dgv_alarmData.AutoGenerateColumns = false;
            //初始化时间条
            InitTimePicker();
            //初始化报警类型下拉选择框
            InitAlarmType();
            // 加载报警信息
            LoadAlarmList();
        }
        /// <summary>
        /// 初始化时间条件
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void InitTimePicker()
        {
            //开始时间   ---  设置为最2000年1月1日
            datatime_start.Value = new DateTime(2020,1,1);
        }

        /// <summary>
        /// 初始化报警类型下拉选择框
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void InitAlarmType()
        {

            var alarmTypeList = new List<dynamic>
            {
                new { Text = "---请选择---", Value = 0 },
                new { Text = "安全类", Value = 1 },
                new { Text = "设备类", Value = 2 },
                new { Text = "工艺类", Value = 3 },
                new { Text = "缓存类", Value = 4 }
            };
            
            cbx_alarmType.DataSource = alarmTypeList;
            cbx_alarmType.DisplayMember = "Text";
            cbx_alarmType.ValueMember = "Value";
            cbx_alarmType.SelectedIndex = 0;
        }

        /// <summary>
        /// 加载报警信息
        /// </summary>
        public void LoadAlarmList()
        {
            string alarmType;
            if((int)cbx_alarmType.SelectedValue == 0)
            {
                alarmType = null;
            }
            else
            {
                var selectedItem = cbx_alarmType.SelectedItem as dynamic;
                alarmType = selectedItem.Text;
            }
            DateTime startTime = datatime_start.Value.Date;
            DateTime endTime = datatime_end.Value.Date;
            List<AlarmModel> alarmList = alarmBLL.GetAlarmList(alarmType,startTime,endTime,1);
            dgv_alarmData.DataSource = alarmList;
        }

        /// <summary>
        /// 查询按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_select_Click(object sender, EventArgs e)
        {
            LoadAlarmList();
        }
    }
}
